var e = require, t = module, n = exports;

Object.defineProperty(n, "__esModule", {
    value: !0
}), n.Grid = void 0;

var o = function() {
    this.x = 0, this.y = 0, this.f = 0, this.g = 0, this.h = 0, this.parent = null, 
    this.type = 0, this.target = null;
};

n.Grid = o;

var i = function() {
    function t() {
        this._gridW = 0, this._gridH = 0, this.mapW = 0, this.mapH = 0, this.is8dir = !0, 
        this.gridsList = null;
    }
    return t.prototype.init = function(t, i, r, h, s) {
        void 0 === s && (s = !0), this._gridW = t, this._gridH = i, this.mapW = r, this.mapH = h, 
        this.is8dir = s, this.initMap();
    }, t.prototype.initMap = function() {
        this.gridsList = new Array(this.mapW + 1);
        for (var t = 0; t < this.gridsList.length; t++) this.gridsList[t] = new Array(this.mapH + 1);
        for (t = 0; t <= this.mapW; t++) for (var i = 0; i <= this.mapH; i++) this.addGrid(t, i, 0);
    }, t.prototype.resetMap = function() {
        this.gridsList.forEach(function(t) {
            t.forEach(function(t) {
                t.type = 0, t.f = 0, t.g = 0, t.h = 0, t.parent = null;
            });
        });
    }, t.prototype.addGrid = function(t, i, r) {
        var h = new o();
        h.x = t, h.y = i, h.type = r, this.gridsList[t][i] = h;
    }, t.prototype.addOther = function(t, i, r) {
        var h, s = t.x - i / 2, a = t.x + i / 2, o = t.y - r / 2, e = t.y + r / 2, n = 0, p = this.mapW;
        i >= this._gridW / 4 ? (g = Math.floor(t.x / this._gridW), h = s % this._gridW, 
        i = a % this._gridW, h < 3 * this._gridW / 4 ? n = Math.max(Math.floor(s / this._gridW), 0) : (n = Math.max(Math.floor(s / this._gridW) + 1, 0)) > g && (n = g), 
        i < this._gridW / 4 ? (p = Math.min(Math.floor(a / this._gridW) - 1, this.mapW)) < g && (p = g) : p = Math.min(Math.floor(a / this._gridW), this.mapW)) : n = p = Math.floor(t.x / this._gridW);
        var g, f = 0, d = this.mapH;
        r >= this._gridH / 2 ? (g = Math.floor(t.y / this._gridH), a = o % this._gridH, 
        r = e % this._gridH, a < 3 * this._gridH / 4 ? f = Math.max(Math.floor(o / this._gridH), 0) : (f = Math.max(Math.floor(o / this._gridH) + 1, 0)) > g && (f = g), 
        r < this._gridH / 4 ? (d = Math.min(Math.floor(e / this._gridH) - 1, this.mapH)) < g && (d = g) : d = Math.min(Math.floor(e / this._gridH), this.mapH)) : f = d = Math.floor(t.y / this._gridH);
        for (var c = n; c <= p; c++) for (var y = f; y <= d; y++) 0 == this.gridsList[c][y].type && (this.gridsList[c][y].type = -1);
    }, t.prototype.retrieve = function(t) {
        return this.retrievePos(cc.v2(t.xMin, t.yMin), cc.v2(t.xMax, t.yMax));
    }, t.prototype.retrievePos = function(t, i) {
        for (var r = this.getGird(t), t = this.getGird(i), i = Math.min(r.x, t.x), h = Math.max(r.x, t.x), s = Math.min(r.y, t.y), a = Math.max(r.y, t.y), o = [], e = i; e <= h; e++) for (var n = s; n <= a; n++) {
            var p = this.gridsList[e][n];
            p && p.target && o.push(p.target);
        }
        return o;
    }, t.prototype.getGirdData = function(t) {
        return t.x < 0 ? t.x = 0 : t.x >= this.gridsList.length && (t.x = this.gridsList.length - 1), 
        t.y < 0 ? t.y = 0 : t.y >= this.gridsList[t.x].length && (t.y = this.gridsList[t.x].length - 1), 
        this.gridsList[t.x][t.y];
    }, t.prototype.getGirdX = function(t) {
        t = Math.round(t / this._gridW - .5) + Math.floor(this.mapW / 2 + .5);
        return t < 0 ? 0 : t > this.mapW ? this.mapW : t;
    }, t.prototype.getGirdY = function(t) {
        t = Math.round(t / this._gridH - .5) + Math.floor(this.mapH / 2 + .5);
        return t < 0 ? 0 : t > this.mapH ? this.mapH : t;
    }, t.prototype.getGird = function(t) {
        return cc.v2(this.getGirdX(t.x), this.getGirdY(t.y));
    }, t.prototype.getX = function(t) {
        return (t - Math.floor(this.mapW / 2 + .5) + .5) * this._gridW;
    }, t.prototype.getY = function(t) {
        return (t - Math.floor(this.mapH / 2 + .5) + .5) * this._gridH;
    }, t.prototype.getPos = function(t) {
        return cc.v2(this.getX(t.x), this.getY(t.y));
    }, t.prototype._sortFunc = function(t, i) {
        return t.f - i.f;
    }, t.prototype.generatePath = function(t) {
        var i = [];
        for (i.push(t); t.parent; ) t = t.parent, i.push(t);
        return i;
    }, t.prototype.findPath = function(t, i) {
        for (var r = [], h = [], s = [], a = new Array(this.mapW + 1), o = 0; o < a.length; o++) a[o] = new Array(this.mapH + 1);
        for (o = 0; o <= this.mapW; o++) for (var e = 0; e <= this.mapH; e++) a[o][e] = Object.assign({}, this.gridsList[o][e]);
        var n = a[t.x][t.y], p = a[i.x][i.y];
        n.type = 1, p.type = 2, n.h = Math.abs(i.x - t.x) + Math.abs(i.y - t.y);
        var g = n;
        for (h.push(g); ;) {
            for (var f = -1; f <= 1; f++) for (var d = -1; d <= 1; d++) if ((0 != f || 0 != d) && (o = g.x + f, 
            e = g.y + d, 0 <= o && 0 <= e && o <= this.mapW && e <= this.mapH && -1 != a[o][e].type && -1 == s.indexOf(a[o][e]))) {
                if (this.is8dir) {
                    if (-1 == a[o - f][e].type || -1 == a[o][e - d].type) continue;
                } else if (0 !== f && 0 !== d) continue;
                var c = 0 !== f && 0 !== d ? g.g + 14 : g.g + 10;
                (0 == a[o][e].g || a[o][e].g > c) && (a[o][e].g = c, a[o][e].parent = g), a[o][e].h = Math.abs(i.x - o) + Math.abs(i.y - e), 
                a[o][e].f = a[o][e].g + a[o][e].h, h.indexOf(a[o][e]) < 0 && h.push(a[o][e]);
            }
            if (s.push(h.shift()), 0 === h.length) {
                var y = s.concat(h).sort(function(t, i) {
                    return t.h - i.h;
                });
                return r = 0 < y.length && 1 != y[0].type && y[0].h < n.h ? this.generatePath(y[0]) : r;
            }
            if (h.sort(this._sortFunc), 2 == (g = h[0]).type) return this.generatePath(g);
        }
    }, t.prototype.floyd = function(t) {
        var i = t.slice(0), r = i.length;
        if (2 < r) for (var h = cc.v2(i[r - 1].x - i[r - 2].x, i[r - 1].y - i[r - 2].y), s = r - 3; 0 <= s; s--) {
            var a = cc.v2(i[s + 1].x - i[s].x, i[s + 1].y - i[s].y);
            h.equals(a) ? i.slice(s + 1, 1) : h = a;
        }
        if (2 < (r = i.length)) for (s = r - 1; 0 <= s; s--) for (var o = 0; o <= s - 2; o++) if (!this.hasBlock(i[s], i[o])) {
            for (var e = s - 1; o < e; e--) i.splice(e, 1);
            s = o, r = i.length;
            break;
        }
    }, t.prototype.hasBlock = function(t, i) {
        var r = this;
        if (t.x == i.x && t.y == i.y) return !1;
        var h = Math.min(t.x, i.x), s = Math.max(t.x, i.x), a = Math.min(t.y, i.y), o = Math.max(t.y, i.y), e = [];
        this.gridsList.forEach(function(t) {
            return e = e.concat(t.filter(function(t) {
                return -1 == t.type && t.x >= h && t.x <= s && t.y >= a && t.y <= o;
            }));
        });
        var n = this.getPos(t), p = this.getPos(i), t = Math.sqrt(Math.pow(this._gridW, 2) + Math.pow(this._gridH, 2)), i = cc.v2(), g = cc.v2(), f = cc.v2(), d = cc.v2(), c = cc.v2(), c = n.x < p.x ? (i = p.sub(n).rotateSelf(Math.PI / 2).normalizeSelf().scaleSelf(cc.v2(t / 2, t / 2)), 
        g = n.add(i), f = p.add(i), d = p.sub(i), n.sub(i)) : (i = n.sub(p).rotateSelf(Math.PI / 2).normalizeSelf().scaleSelf(cc.v2(t / 2, t / 2)), 
        g = p.add(i), f = n.add(i), d = n.sub(i), p.sub(i));
        return e.some(function(t) {
            t = r.getPos(t);
            return cc.Intersection.rectPolygon(cc.rect(t.x - r._gridW / 2, t.y - r._gridH / 2, r._gridW, r._gridH), [ g, f, d, c ]);
        });
    }, t;
}();

n.default = i;